// PROGRAMA : ESTPC131
// BIBLIOTECA : PRODETR
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : SEJUSP - SEC. DA JUSTICA E SEGURANCA PUBLICA
// INSTITUTO DE IDENTIFICACAO
// PROGRAMADOR : EDU/FM DATA: 30/06/92
// OBJETIVO :
// OBJETIVO : CONSULTA/EMISSAO QUADRO ESTATISTICO. OPCAO: "CEE"
// ID. CIVIL
// ATUALIZACOES DATA
// PROGRAMA : ESTPC131 03/11/1997
// BIBLIOTECA : PRODETR
// PROGRAMADOR :
// OBJETIVO : ADAPTACAO PARA O SHOPPING DO CIDADAO
// OBS : ESTE PROGRAMA FOI REESTRUTURADO POR SOLON MIRANDA
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.Arrays;

import net.sf.jasperreports.engine.JRException;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.download.InputStreamDownload;
import br.com.vti.sisp_sicrim.antigo.controller.relatorios.Estte131Report;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Estpc131A;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Estpc131B;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Estpc131C;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Esttc131;
import br.com.vti.sisp_sicrim.antigo.model.SicEstatistica;
import br.com.vti.sisp_sicrim.antigo.model.SicTabelas;
import br.com.vti.sisp_sicrim.antigo.model.Sislac01;
import br.com.vti.sisp_sicrim.antigo.model.Sislct01;
import br.com.vti.sisp_sicrim.antigo.model.Sisldat8;
import br.com.vti.sisp_sicrim.antigo.model.Sisll001;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Estpc131 OBJETIVO : OBJETIVO : CONSULTA/EMISSAO QUADRO ESTATISTICO. OPCAO: "CEE" ID. CIVIL *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/estpc131")
public class Estpc131 {
	// Views utilizadas
	SicEstatistica vwCadest = new SicEstatistica();
	SicTabelas vwTabele = new SicTabelas();
	// Telas/relatorios utilizados
	Estte131Report report;
	Estpc131A estpc131A;
	Estpc131B estpc131B;
	Estpc131C estpc131C;
	Esttc131 esttc131;
	// Includes utilizados
	Sislac01 sislac01 = new Sislac01();
	Sislct01 sislct01 = new Sislct01();
	Sisldat8 sisldat8 = new Sisldat8();
	Sisll001 sisll001 = new Sisll001();
	// Areas globais / session
	Sisgsisp sisgsisp;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Estpc131 estpc131Anterior;
	String faseproc = "0";

	private int[] mat0 = new int[16];
	private int[][] tra0 = new int[8][16];
	private int[] tot0 = new int[8];
	private int[] mat1 = new int[101];
	private String[] conimp = new String[3];
	private int[][] tra1 = new int[8][101];
	private int tempo = 0;
	private int k = 0;
	private int i = 0;
	private String motida = "";
	private String motid2 = "";
	private String[] tabm = { "", "   ", "VEI", "CON", "MUL", "   ", "IPV", "ROU", "   ", "   ", "   ", "   ", "   ", "IDC", "ICR", "FUN", "   ", "   " };

	private Estpc131A preMapas() {
		estpc131A = new Estpc131A(true, sessao);
		estpc131B = new Estpc131B(true, sessao);
		estpc131C = new Estpc131C(true, sessao);
		esttc131 = new Esttc131(true, sessao);
		estpc131A.setDataCorrente(UtilBean.obtemDataDia());
		estpc131A.setNomeClasse(this.getClass().getSimpleName().toUpperCase());
		esttc131.setDataCorrente(UtilBean.obtemDataDia());
		esttc131.setNomeClasse(this.getClass().getSimpleName().toUpperCase());

		Arrays.fill(mat0, 0);
		Arrays.fill(tot0, 0);
		Arrays.fill(mat1, 0);
		Arrays.fill(conimp, "");
		// Par�grafo: rp0()
		return estpc131A;
	}

	public Estpc131(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public InputStreamDownload executar(Estpc131A estpc131A, Estpc131B estpc131B, Estpc131C estpc131C, Esttc131 esttc131) {
		try {
			return main(estpc131A, estpc131B, estpc131C, esttc131);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroSiscerro(e, sessao, this.getClass().getSimpleName());
		}
		return null;
	}

	private InputStreamDownload main(Estpc131A pestpc131A, Estpc131B pestpc131B, Estpc131C pestpc131C, Esttc131 pesttc131) throws Exception {
		if (pestpc131A == null && pestpc131B == null && pestpc131C == null && pesttc131 == null) {
			primeiraVez = true;
			// esttc131 = new Esttc131 (primeiraVez);
			estpc131A = preMapas();
			estpc131A.setNavegar("../estpc131/executar");
			result.include("estpc131A", estpc131A);
			sessao.setAtributo("ESTPC131", this);
			estpc131A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		} else {
			restaurarEstados(pestpc131A, pestpc131B, pestpc131C, pesttc131);
		}

		report = new Estte131Report();
		if (esttc131.getOperacao().equals("CAD")) {
			funcoes.sissm001(result, sessao);
		}

		if (pestpc131A != null) {
			estpc131A.receberTela(false, result);
		}
		if (!UtilBean.funcao.equals(UtilBean.ctt_processa)) {
			estpc131A.enviarMensagem(null, "");
			result.include("estpc131A", estpc131A);
			sessao.setAtributo("ESTPC131", this);
			estpc131A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}
		if (!esttc131.getOperacao().equals("CEE")) {
			funcoes.sissm001(result, sessao);
			return null;
		}
		if (!UtilBean.validarMascara(estpc131A.getDatini(), "yyyyMMdd")) {
			estpc131A.enviarMensagem("ddt", "");
			result.include("estpc131A", estpc131A);
			sessao.setAtributo("ESTPC131", this);
			estpc131A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}
		if (estpc131A.getAdt() < 1992) {
			estpc131A.enviarMensagem("adt", "");
			result.include("estpc131A", estpc131A);
			sessao.setAtributo("ESTPC131", this);
			estpc131A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}
		if (!UtilBean.validarMascara(estpc131A.getDatfim(), "yyyyMMdd")) {
			estpc131A.enviarMensagem("dfi", "");
			result.include("estpc131A", estpc131A);
			sessao.setAtributo("ESTPC131", this);
			estpc131A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}
		if (estpc131A.getAfi() < 1992) {
			estpc131A.enviarMensagem("afi", "");
			result.include("estpc131A", estpc131A);
			sessao.setAtributo("ESTPC131", this);
			estpc131A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}
		if (estpc131A.getDatini() > estpc131A.getDatfim()) {
			estpc131A.enviarMensagem("adt", "");
			result.include("estpc131A", estpc131A);
			sessao.setAtributo("ESTPC131", this);
			estpc131A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}

		tempo = estpc131A.getDatfim() - estpc131A.getDatini();

		motida = UtilBean.moveSubstring(motida, 0, 3, "A", tabm[sessao.getSisgsisp().getVar()]);
		motida = UtilBean.moveSubstring(motida, 3, 2, "N", 99);
		motida = UtilBean.moveSubstring(motida, 5, 8, "N", estpc131A.getDatini());
		motid2 = motida;
		motid2 = UtilBean.moveSubstring(motid2, 5, 8, "N", estpc131A.getDatfim());
		funcoes.tabssup2(sessao);

		if (UtilBean.validaSelecao(estpc131A.getConimp())) {
			estpc131A.enviarMensagem("conimp[1]", "");
			result.include("estpc131A", estpc131A);
			sessao.setAtributo("ESTPC131", this);
			estpc131A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}
		esttc131.setDtini(estpc131A.getDatini());
		esttc131.setDtini((int) (funcoes.sissd002(esttc131.getDtini())));
		esttc131.setDtfim(estpc131A.getDatfim());
		esttc131.setDtfim((int) (funcoes.sissd002(esttc131.getDtfim())));
		k = 1;
		esttc131.setMensagem(""); // mens = "";
		if (!estpc131A.getConimp(2).trim().equals("")) {
			return relatorio();
		} else {
			if (estpc131A.getMat() > 0) {
				sislct01.tt1 = 18;
				sislct01.cc1 = estpc131A.getMat();
				sislct01.vt1 = funcoes.sisnct01(sislct01.tt1, sislct01.cc1, sislct01.vt1);
				if (sislct01.vt1.equals("@")) {
					esttc131.setMensagemSaida(" REGISTRO NAO ENCONTRADO");
					sessao.setAtributo("ESTPC131", this);
					result.include("esttc131", esttc131);
					esttc131.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
					return null;
				}
				soma();
				tela();
				for (int i1 = 1; i1 <= 7; i1++) {
					for (int i2 = 1; i2 <= 15; i2++) {
						tra0[i1][i2] = 0;
					}
				}
				for (int i1 = 1; i1 <= 15; i1++) {
					mat0[i1] = 0;
				}
				k = 0;
				motida = UtilBean.moveSubstring(motida, 13, 7, "N", 0);
				for (int i1 = 1; i1 <= 2; i1++) {
					conimp[i1] = "";
				}
				for (int i1 = 1; i1 <= 7; i1++) {
					for (int i2 = 1; i2 <= 100; i2++) {
						tra1[i1][i2] = 0;
					}
				}
				for (int i1 = 1; i1 <= 7; i1++) {
					tot0[i1] = 0;
				}
				// matric = 0;
				// datini = 0;
				// datfim = 0;
				sessao.setAtributo("ESTPC131", this);
				result.include("esttc131", esttc131);
				esttc131.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return null;
			}
			while (vwTabele.pesquisaCodigoTabela(18)) {
				if (vwTabele.fimDados()) {
					tela();
				}
				sislac01.niveis = vwTabele.getDescricaoElem();
				if (!(UtilBean.subStr(sislac01.niveis, 30 + sessao.getSisgsisp().getVar() - 1, 1).compareTo("0") > 0)) {
					continue;
				}

				estpc131A.setMat(vwTabele.getCodigoElem());
				soma();

			}
			vwTabele.fecha();
			sessao.setAtributo("ESTPC131", this);
			result.include("esttc131", esttc131);
			esttc131.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
		}
		return null;
	}

	private void soma() {
		while (vwCadest.pesquisaMotidaFaixa(tabm[sessao.getSisgsisp().getVar()], 97, (int) estpc131A.getMat(), sisldat8.datini, sisldat8.datfim)) {

			// estpc131A.enviarReceberTela(false, result);
			if (estpc131A.getMat() > 0) {
				if (!(vwCadest.getMatriculaFunc() == estpc131A.getMat())) {
					continue;
				}
			}
			if (esttc131.getMat0(k) != vwCadest.getMatriculaFunc()) {
				if (esttc131.getMat0(k) == 0) {
					esttc131.setMat0(k, vwCadest.getMatriculaFunc());
				} else {
					k += 1;
					esttc131.setMat0(k, vwCadest.getMatriculaFunc());
				}
			}
			for (i = 1; i <= 6; i++) {

				esttc131.setTra0(i, k, (int) (esttc131.getTra0(i, k) + vwCadest.getQuantTrans(i)));
				esttc131.setTra0(7, k, (int) (esttc131.getTra0(7, k) + vwCadest.getQuantTrans(i)));
				esttc131.setTot0(i, (int) (esttc131.getTot0(i) + vwCadest.getQuantTrans(i)));
				esttc131.setTot0(7, (int) (esttc131.getTot0(7) + vwCadest.getQuantTrans(i)));
			}
			if (k == 15) {
				tela();
			}

		}
		vwCadest.fecha();
		if (vwCadest.registrosProcessados() == 0) {
			estpc131A.setMensagemSaida("INTERVALO INEXISTENTE");
		}
	}

	private void tela() {
		estpc131A.setMensagemSaida("CONSULTA EFETUADA OK");

		esttc131.receberTela(false, result);
		for (int i1 = 1; i1 <= 7; i1++) {
			for (int i2 = 1; i2 <= 15; i2++) {
				tra0[i1][i2] = 0;
			}
		}
		for (int i1 = 1; i1 <= 15; i1++) {
			mat0[i1] = 0;
		}
		motida = UtilBean.moveSubstring(motida, 13, 7, "N", 0);
		// matric = 0;
		// datini = 0;
		// datfim = 0;
		for (int i1 = 1; i1 <= 2; i1++) {
			conimp[i1] = "";
		}
		k = 1;
	}

	private InputStreamDownload relatorio() throws ParseException, JRException, FileNotFoundException {
		if (estpc131A.getMat() > 0) {
			sislct01.tt1 = 18;
			sislct01.cc1 = estpc131A.getMat();

			report.setHoraCorrente(esttc131.getHoraCorrente());

			funcoes.sisnct01(sislct01.tt1, sislct01.cc1, sislct01.vt1);
			if (sislct01.vt1.equals("@")) {
				estpc131A.setMensagemSaida(" REGISTRO NAO ENCONTRADO");
				return null;
			}
			mais();
			impr();
			for (int i1 = 1; i1 <= 7; i1++) {
				for (int i2 = 1; i2 <= 15; i2++) {
					tra0[i1][i2] = 0;
				}
			}
			for (int i1 = 1; i1 <= 15; i1++) {
				mat0[i1] = 0;
			}
			k = 0;
			motida = UtilBean.moveSubstring(motida, 13, 7, "N", 0);
			for (int i1 = 1; i1 <= 2; i1++) {
				conimp[i1] = "";
			}
			for (int i1 = 1; i1 <= 7; i1++) {
				for (int i2 = 1; i2 <= 100; i2++) {
					tra1[i1][i2] = 0;
				}
			}
			for (int i1 = 1; i1 <= 7; i1++) {
				tot0[i1] = 0;
			}
			// matric = 0;
			// datini = 0;
			// datfim = 0;
			// exit();
			return impr();
		}
		while (vwTabele.pesquisaCodigoTabela(18)) {
			sislac01.niveis = vwTabele.getDescricaoElem();
			if (!(UtilBean.subStr(sislac01.niveis, 30 + sessao.getSisgsisp().getVar() - 1, 1).compareTo("0") > 0)) {
				continue;
			}

			estpc131A.setMat(vwTabele.getCodigoElem());
			mais();
		}
		if (vwTabele.fimDados()) {
			impr();
		}
		vwTabele.fecha();
		return impr();
	}

	private void mais() {
		while (vwCadest.pesquisaMotidaFaixa(tabm[sessao.getSisgsisp().getVar()], 97, (int) estpc131A.getMat(), sisldat8.datini, sisldat8.datfim)) {

			// estpc131A.enviarReceberTela(false, result);
			if (estpc131A.getMat() > 0) {
				if (!(vwCadest.getMatriculaFunc() == estpc131A.getMat())) {
					continue;
				}
			}
			if (mat1[k] != vwCadest.getMatriculaFunc()) {
				if (mat1[k] == 0) {

					mat1[k] = vwCadest.getMatriculaFunc();
				} else {
					k += 1;

					mat1[k] = vwCadest.getMatriculaFunc();
				}
			}
			for (i = 1; i <= 6; i++) {

				tra1[i][k] = (int) (tra1[i][k] + vwCadest.getQuantTrans(i));
				tra1[7][k] = (int) (tra1[7][k] + vwCadest.getQuantTrans(i));
				esttc131.setTot0(i, (int) (esttc131.getTot0(i) + vwCadest.getQuantTrans(i)));
				esttc131.setTot0(7, (int) (esttc131.getTot0(7) + vwCadest.getQuantTrans(i)));
			}

		}
		vwCadest.fecha();
		if (vwCadest.registrosProcessados() == 0) {
			estpc131A.setMensagemSaida("INTERVALO INEXISTENTE");
		}
	}

	private InputStreamDownload impr() throws ParseException, JRException, FileNotFoundException {
		estpc131A.setMensagemSaida("EMISSAO EFETUADA OK");
		// RELATORIO
		report.gravaRelatorio(1);

		// RELATORIO
		for (int i1 = 1; i1 <= 100; i1++) {
			mat1[i1] = 0;
		}
		for (int i1 = 1; i1 <= 7; i1++) {
			for (int i2 = 1; i2 <= 100; i2++) {
				tra1[i1][i2] = 0;
			}
		}
		for (int i1 = 1; i1 <= 7; i1++) {
			tot0[i1] = 0;
		}
		motida = UtilBean.moveSubstring(motida, 13, 7, "N", 0);
		// matric = 0;
		// datini = 0;
		// datfim = 0;
		for (int i1 = 1; i1 <= 2; i1++) {
			conimp[i1] = "";
		}

		return report.imprimir();
	}

	private void restaurarEstados(Estpc131A pestpc131A, Estpc131B pestpc131B, Estpc131C pestpc131C, Esttc131 pesttc131) {
		this.estpc131Anterior = (Estpc131) sessao.getAtributo("ESTPC131");
		if (pestpc131A != null) {
			estpc131A = pestpc131A;
		} else {
			estpc131A = this.estpc131Anterior.estpc131A;
		}
		estpc131A.setSessao(sessao);
		if (pestpc131B != null) {
			estpc131B = pestpc131B;
		} else {
			estpc131B = this.estpc131Anterior.estpc131B;
		}
		estpc131B.setSessao(sessao);
		if (pestpc131C != null) {
			estpc131C = pestpc131C;
		} else {
			estpc131C = this.estpc131Anterior.estpc131C;
		}
		estpc131C.setSessao(sessao);
		if (pesttc131 != null) {
			esttc131 = pesttc131;
		} else {
			esttc131 = this.estpc131Anterior.esttc131;
		}
		esttc131.setSessao(sessao);
		this.faseproc = this.estpc131Anterior.faseproc;
	}
}
